﻿using System;

namespace Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL
{
    public class Field
    {
        public enum ContentTypes
        {
            text = 0,
            xml = 1,
            html = 2,
            xhtml = 3
        }

        public enum Types
        {
            none = 0,
            number = 1,
            floatingpoint = 2,
            datetime = 3,
            boolean = 4,
        }

        #region private variables

        private System.Xml.Linq.XElement m_XElement;
        private System.Xml.Linq.XElement m_ExtensionXElement;

        private string m_Element;
        private string m_Label;
        private string m_DefaultValue;
        private string m_PickListID;

        private bool m_Required;
        private bool m_Hidden;
        private bool m_Array;
        private bool m_ReadOnly;
        private bool m_Multiline;

        private Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Binding m_Binding;
        private Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Field.ContentTypes m_ContentType;
        private Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Field.Types m_Type;

        #endregion


        public Field(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Binding i_Binding, string i_Element)
            : this(i_Binding, System.String.Empty, i_Element, System.String.Empty, System.String.Empty, false, false, false, false, false, Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Field.ContentTypes.text, Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Field.Types.none)
        {
        }

        public Field(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Binding i_Binding, string i_PickListID, string i_Element)
            : this(i_Binding, i_PickListID, i_Element, System.String.Empty, System.String.Empty, false, false, false, false, false, Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Field.ContentTypes.text, Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Field.Types.none)
        {
        }

        public Field(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Binding i_Binding, string i_Element, string i_Label, bool i_Required, Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Field.ContentTypes i_ContentType)
            : this(i_Binding, System.String.Empty, i_Element, i_Label, System.String.Empty, i_Required, false, false, false, false, i_ContentType, Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Field.Types.none)
        {
        }

        public Field(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Binding i_Binding, string i_PickListID, string i_Element, string i_Label, bool i_Required, Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Field.ContentTypes i_ContentType)
            : this(i_Binding, i_PickListID, i_Element, i_Label, System.String.Empty, i_Required, false, false, false, false, i_ContentType, Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Field.Types.none)
        {
        }

        public Field(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Binding i_Binding, string i_PickListID, string i_Element, string i_Label, string i_DefaultValue, bool i_Required, bool i_Hidden, bool i_Array, bool i_ReadOnly, bool i_Multiline, Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Field.ContentTypes i_ContentType, Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Field.Types i_Type)
        {
            this.FieldInitialize
                (
                i_PickListID,
                i_Element,
                i_Label,
                i_DefaultValue,
                i_Required,
                i_Hidden,
                i_Array,
                i_ReadOnly,
                i_Multiline,
                i_ContentType,
                i_Type
                );

            m_XElement = new System.Xml.Linq.XElement(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.SimpleFeedBindingsXNamespace + Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.FIELD_ELEMENT_NAME);

            m_XElement.SetAttributeValue(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.ELEMENT_ATTRIBUTE, m_Element);

            if (!System.String.IsNullOrEmpty(m_PickListID))
                m_XElement.SetAttributeValue(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.PICKLIST_ID_ATTRIBUTE, m_PickListID);

            if (m_ContentType != Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Field.ContentTypes.text)
            {
                string ContentType;

                switch (m_ContentType)
                {
                    case Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Field.ContentTypes.xml:
                        {
                            ContentType = Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.XML_CONTENT_TYPE;
                            break;
                        }

                    case Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Field.ContentTypes.html:
                        {
                            ContentType = Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.HTML_CONTENT_TYPE;
                            break;
                        }

                    case Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Field.ContentTypes.xhtml:
                        {
                            ContentType = Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.XHTML_CONTENT_TYPE;
                            break;
                        }

                    default:
                        {
                            throw new System.ArgumentException("Invalid value for m_ContentType!");
                        }
                }

                m_XElement.SetAttributeValue(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.CONTENT_TYPE_ATTRIBUTE, ContentType);
            }

            if (m_Type != Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Field.Types.none)
            {
                string Type;

                switch (m_Type)
                {
                    case Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Field.Types.number:
                        {
                            Type = Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.NUMBER_TYPE;
                            break;
                        }

                    case Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Field.Types.floatingpoint:
                        {
                            Type = Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.FLOATINGPOINT_TYPE;
                            break;
                        }

                    case Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Field.Types.datetime:
                        {
                            Type = Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.DATETIME_TYPE;
                            break;
                        }

                    case Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Field.Types.boolean:
                        {
                            Type = Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.BOOLEAN_TYPE;
                            break;
                        }

                    default:
                        {
                            throw new System.ArgumentException("Invalid value for m_Type!");
                        }
                }

                m_XElement.SetAttributeValue(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.TYPE_ATTRIBUTE, Type);
            }

            if (!System.String.IsNullOrEmpty(m_Label))
                m_XElement.SetAttributeValue(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.LABEL_ATTRIBUTE, m_Label);

            if (!System.String.IsNullOrEmpty(m_DefaultValue))
                m_XElement.SetAttributeValue(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.DEFAULT_VALUE_ATTRIBUTE, m_DefaultValue);

            if (m_Required)
                m_XElement.SetAttributeValue(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.REQUIRED_ATTRIBUTE, true.ToString().ToLower());

            if (m_Hidden)
                m_XElement.SetAttributeValue(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.HIDDEN_ATTRIBUTE, true.ToString().ToLower());

            if (m_ReadOnly)
                m_XElement.SetAttributeValue(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.READ_ONLY_ATTRIBUTE, true.ToString().ToLower());

            if (m_Array)
                m_XElement.SetAttributeValue(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.ARRAY_ATTRIBUTE, true.ToString().ToLower());

            if (m_Multiline)
                m_XElement.SetAttributeValue(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.MULTILINE_ATTRIBUTE, true.ToString().ToLower());
        }

        public Field(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Binding i_Binding, System.Xml.Linq.XElement i_FieldXElement)
        {
            if (i_FieldXElement == null)
                throw new System.ArgumentException("i_FieldXElement must be non-null!");

            bool InvalidElement =
                (i_FieldXElement.Name.LocalName != Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.FIELD_ELEMENT_NAME) ||
                (i_FieldXElement.Name.NamespaceName != Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.SIMPLE_FEED_BINDINGS_XML_NAMESPACE_URI);

            if (InvalidElement)
                throw new System.ArgumentException("Invalid i_FieldXElement!");

            m_XElement = i_FieldXElement;

            string Element = m_XElement.Attribute(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.ELEMENT_ATTRIBUTE).Value;

            string PickListID = System.String.Empty;
            if (m_XElement.Attribute(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.PICKLIST_ID_ATTRIBUTE) != null)
                PickListID = m_XElement.Attribute(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.PICKLIST_ID_ATTRIBUTE).Value;

            string ContentType = System.String.Empty;
            if (m_XElement.Attribute(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.CONTENT_TYPE_ATTRIBUTE) != null)
                ContentType = m_XElement.Attribute(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.CONTENT_TYPE_ATTRIBUTE).Value;

            string Type = System.String.Empty;
            if (m_XElement.Attribute(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.TYPE_ATTRIBUTE) != null)
                Type = m_XElement.Attribute(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.TYPE_ATTRIBUTE).Value;

            string Label = System.String.Empty;
            if (m_XElement.Attribute(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.LABEL_ATTRIBUTE) != null)
                Label = m_XElement.Attribute(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.LABEL_ATTRIBUTE).Value;

            string DefaultValue = System.String.Empty;
            if (m_XElement.Attribute(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.DEFAULT_VALUE_ATTRIBUTE) != null)
                DefaultValue = m_XElement.Attribute(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.DEFAULT_VALUE_ATTRIBUTE).Value;

            bool Required = false;
            if (m_XElement.Attribute(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.REQUIRED_ATTRIBUTE) != null)
                Required = System.Convert.ToBoolean(m_XElement.Attribute(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.REQUIRED_ATTRIBUTE).Value);

            bool Hidden = false;
            if (m_XElement.Attribute(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.HIDDEN_ATTRIBUTE) != null)
                Hidden = System.Convert.ToBoolean(m_XElement.Attribute(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.HIDDEN_ATTRIBUTE).Value);

            bool ReadOnly = false;
            if (m_XElement.Attribute(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.READ_ONLY_ATTRIBUTE) != null)
                ReadOnly = System.Convert.ToBoolean(m_XElement.Attribute(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.READ_ONLY_ATTRIBUTE).Value);

            bool Array = false;
            if (m_XElement.Attribute(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.ARRAY_ATTRIBUTE) != null)
                Array = System.Convert.ToBoolean(m_XElement.Attribute(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.ARRAY_ATTRIBUTE).Value);

            bool Multiline = false;
            if (m_XElement.Attribute(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.MULTILINE_ATTRIBUTE) != null)
                Multiline = System.Convert.ToBoolean(m_XElement.Attribute(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.MULTILINE_ATTRIBUTE).Value);

            this.FieldInitialize
                (
                PickListID,
                Element,
                Label,
                DefaultValue,
                Required,
                Hidden,
                Array,
                ReadOnly,
                Multiline,
                ContentType,
                Type
                );

            System.Xml.Linq.XElement ExtensionXElement = i_FieldXElement.Element(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.SimpleFeedBindingsXNamespace + Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.FIELD_EXTENSION_ELEMENT_NAME);

            this.AddExtensionXElement(ExtensionXElement, false);
        }

        internal void FieldInitialize(string i_PickListID, string i_Element, string i_Label, string i_DefaultValue, bool i_Required, bool i_Hidden, bool i_Array, bool i_ReadOnly, bool i_Multiline, string i_ContentType, string i_Type)
        {
            Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Field.ContentTypes ContentType = Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Field.ContentTypes.text;
            Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Field.Types Type = Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Field.Types.none;

            if (!System.String.IsNullOrEmpty(i_ContentType))
            {
                switch (i_ContentType)
                {
                    case Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.TEXT_CONTENT_TYPE:
                        {
                            ContentType = Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Field.ContentTypes.text;
                            break;
                        }

                    case Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.XML_CONTENT_TYPE:
                        {
                            ContentType = Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Field.ContentTypes.xml;
                            break;
                        }

                    case Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.HTML_CONTENT_TYPE:
                        {
                            ContentType = Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Field.ContentTypes.html;
                            break;
                        }

                    case Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.XHTML_CONTENT_TYPE:
                        {
                            ContentType = Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Field.ContentTypes.xhtml;
                            break;
                        }

                    default:
                        {
                            throw new System.ArgumentException("Invalid value for i_ContentType!");
                        }
                }
            }

            if (!System.String.IsNullOrEmpty(i_Type))
            {
                switch (i_Type)
                {
                    case Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.NONE_TYPE:
                        {
                            Type = Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Field.Types.none;
                            break;
                        }

                    case Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.NUMBER_TYPE:
                        {
                            Type = Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Field.Types.number;
                            break;
                        }

                    case Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.FLOATINGPOINT_TYPE:
                        {
                            Type = Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Field.Types.floatingpoint;
                            break;
                        }

                    case Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.DATETIME_TYPE:
                        {
                            Type = Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Field.Types.datetime;
                            break;
                        }

                    case Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.BOOLEAN_TYPE:
                        {
                            Type = Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Field.Types.boolean;
                            break;
                        }

                    default:
                        {
                            throw new System.ArgumentException("Invalid value for i_Type!");
                        }
                }
            }

            this.FieldInitialize
                (
                i_PickListID,
                i_Element,
                i_Label,
                i_DefaultValue,
                i_Required,
                i_Hidden,
                i_Array,
                i_ReadOnly,
                i_Multiline,
                ContentType,
                Type
                );
        }

        internal void FieldInitialize(string i_PickListID, string i_Element, string i_Label, string i_DefaultValue, bool i_Required, bool i_Hidden, bool i_Array, bool i_ReadOnly, bool i_Multiline, Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Field.ContentTypes i_ContentType, Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Field.Types i_Type)
        {
            if (System.String.IsNullOrEmpty(i_Element) || (i_Element.IndexOf("\\") != -1))
                throw new System.ArgumentException("Invalid value for i_Element!");

            string[] ElementNames = i_Element.Split('/');
            string[] NamespaceIDs = new string[0];

            m_Element = i_Element;
            m_PickListID = i_PickListID;

            //  Strip off trailing "/" if it is the last character
            if (m_Element.LastIndexOf("/") == m_Element.Length - 1)
                m_Element = m_Element.Substring(0, m_Element.Length - 1);

            if (System.String.IsNullOrEmpty(i_Label))
                m_Label = m_Element;
            else
                m_Label = i_Label;

            m_DefaultValue = i_DefaultValue;

            m_Required = i_Required;
            m_Hidden = i_Hidden;
            m_Array = i_Array;
            m_ReadOnly = i_ReadOnly;
            m_Multiline = i_Multiline;
            m_ContentType = i_ContentType;
            m_Type = i_Type;
        }

        public Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Binding Binding
        {
            get
            {
                return m_Binding;
            }

            internal set
            {
                if (value == null)
                    throw new System.ArgumentException("Value must be non-null!");

                m_Binding = value;
            }
        }

        public string Element
        {
            get
            {
                return m_Element;
            }
        }

        public string PickListID
        {
            get
            {
                return m_PickListID;
            }
        }

        public Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Field.ContentTypes ContentType
        {
            get
            {
                return m_ContentType;
            }
        }

        public Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Field.Types Type
        {
            get
            {
                return m_Type;
            }
        }

        public string Label
        {
            get
            {
                return m_Label;
            }
        }

        public string DefaultValue
        {
            get
            {
                return m_DefaultValue;
            }
        }

        public bool Array
        {
            get
            {
                return m_Array;
            }
        }

        public bool Required
        {
            get
            {
                return m_Required;
            }
        }

        public bool Hidden
        {
            get
            {
                return m_Hidden;
            }
        }

        public bool ReadOnly
        {
            get
            {
                return m_ReadOnly;
            }
        }

        public bool Multiline
        {
            get
            {
                return m_Multiline;
            }
        }

        public System.Xml.Linq.XElement XElement
        {
            get
            {
                return m_XElement;
            }
        }

        public System.Xml.Linq.XElement ExtensionXElement
        {
            get
            {
                return m_ExtensionXElement;
            }
        }

        public void AddExtensionXElement(System.Xml.Linq.XElement i_ExtensionXElement)
        {
            this.AddExtensionXElement
                (
                i_ExtensionXElement,
                true
                );
        }

        private void AddExtensionXElement(System.Xml.Linq.XElement i_ExtensionXElement, bool i_AppendXElement)
        {
            if (m_ExtensionXElement != null)
                throw new System.ArgumentException("Extension already exists for field!");

            m_ExtensionXElement = i_ExtensionXElement;

            if (i_AppendXElement)
                m_XElement.Add(i_ExtensionXElement);
        }

        public override string ToString()
        {
            return m_Element;
        }
    }
}
